package com.example.pay.service;

import com.alibaba.fastjson.JSON;
import com.example.enums.MsgEnum;
import com.example.msg.api.MsgApi;
import com.example.msg.dto.MsgDTO;
import com.example.order.api.OrderApi;
import com.example.order.dto.OrderDTO;
import com.example.pay.dto.PayDTO;
import com.example.pay.entity.Pay;
import com.example.pay.mapper.PayMapper;
import com.example.utils.DatetimeUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Random;

@Service
public class PayService {
    @Autowired

    private OrderApi orderApi;
    @Autowired
    private MsgApi msgApi;

    @Value("${spring.application.name}")
    private String appName;

    @Autowired
    private PayMapper payMapper;

    private static final String MQ_SCORE_ROUTING_KEY = "score.routing.key";
    private static final String MQ_ORDER_ROUTING_KEY = "order.routing.key";

    public Pay findPayByOrderNo(String orderNo) {
        Pay pay = new Pay();
        pay.setOrderNo(orderNo);
        pay = this.payMapper.selectOne(pay);
        return pay;
    }

    /**
     * 先创建支付订单
     * @param obj
     * @return
     */
    @Transactional
    public  String createPay(PayDTO obj){
        Pay pay=new Pay();
        BeanUtils.copyProperties(obj,pay);
        //交易单号
        Random rand = new Random();
        //随机 1000-9999的数字
        int n = rand.nextInt(8999)+1000;
        String payno= DatetimeUtil.getCurrentDatetime()+n;
        pay.setPayNo("pay-"+ payno);
        //支付完成
        pay.setStatus((byte)0);
        this.payMapper.insertSelective(pay);
        return pay.getPayNo();
    }

    public Pay findPayByPayNo(String payNo) {
        Pay pay = new Pay();
        pay.setPayNo(payNo);
        pay = this.payMapper.selectOne(pay);
        return pay;
    }

    public  void confirmPayHandle(String payNo){
        Pay pay=new Pay();
        pay.setPayNo(payNo);
        pay=this.payMapper.selectOne(pay);
        //改为待发送
        sendOrderPrepareMsg(pay);

        //3.执行本地业务，对应设计图的步骤3
        this.confirmPay(pay);

        this.sendOrderConfirmMsg(pay.getOrderNo());
    }

    private void sendOrderPrepareMsg(Pay obj){
        MsgDTO msgDTO=new MsgDTO();
        String msgid= MsgEnum.ORDER.getCode()+"-"+obj.getOrderNo();
        msgDTO.setMsgId(msgid);
        msgDTO.setAppName(this.appName);
        msgDTO.setRoutingKey(MQ_ORDER_ROUTING_KEY);
        OrderDTO orderDTO=new OrderDTO();
        orderDTO.setUserId(obj.getUserId());
        orderDTO.setOrderNo(obj.getOrderNo());
        msgDTO.setJsonMsg(JSON.toJSONString(orderDTO));
        this.msgApi.prepareMsg(msgDTO);
    }

    /**
     * 把支付订单号，改为已完成。
     * @param pay
     */
    @Transactional
    public  void confirmPay(Pay pay){
        //设置支付状态 -1：取消 0 未完成 1已完成 -2:异常
        pay.setStatus((byte)1);
        this.payMapper.updateByPrimaryKeySelective(pay);
    }

    private void sendOrderConfirmMsg(String orderNo){
        MsgDTO msgDTO=new MsgDTO();
        String msgid=MsgEnum.ORDER.getCode()+"-"+orderNo;
        msgDTO.setMsgId(msgid);
        this.msgApi.confirmMsg(msgDTO);
    }

}
